import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:xmshop/app/services/searchServices.dart';

// import 'storage.dart';
import '../../../services/storage.dart';

class ShopSearchController extends GetxController {
  String keywords = "";

  RxList historyList = [].obs;

  @override
  void onInit() {
    super.onInit();
    getHistoryData();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void getHistoryData() async {
    var tempList = await SearchServices.getHistoryData();
    if (tempList.isNotEmpty) {
      historyList.addAll(tempList);
      update();
    }
  }

  clearHistoryData() async {
    await SearchServices.clearHistoryData();
    historyList.clear();
    update();
  }

  removeHistoryData(keywords) async {
    var tempList = await SearchServices.getHistoryData();
    tempList.remove(keywords);
    historyList.remove(keywords);
    await Storage.setData("searchList", tempList);
    update();
  }

  setHistoryData(keywords) async {
    SearchServices.setHistoryData(keywords);
  }

  // 显示删除确认对话框
  void showDeleteConfirmDialog(BuildContext context, String keyword) {
    /*   // this.itemId.value = itemId;
    Get.defaultDialog(
      title: '删除确认',
      text: '你确定要删除这个项目吗？',
      confirmText: '确定',
      cancelText: '取消',
      onConfirm: () {
        // 用户点击了确定按钮，执行删除逻辑
        deleteItem(itemId);
        Get.back(); // 关闭对话框
      },
      onCancel: () {
        // 用户点击了取消按钮，什么也不做
        Get.back(); // 关闭对话框
      },
    );*/
  }

  // 实际的删除逻辑
  void deleteItem(String keyword) {
    // 在这里执行删除操作，例如从数据库中删除项目
    print('删除了名字为 $keyword 的项目');
  }
}
